class TableColumn:
    def __init__(self, title):
        self.title = title
        self.maxLen = len(title)
        self.content = []
    def add(self, content):
        if len(content) > self.maxLen:
            self.maxLen = len(content)
        self.content.append(content)
    def clear(self):
        self.maxLen = len(self.title)
        self.content = []
        
class Table:
    def __init__(self, titles): # titles is str list
        if len(titles) == 0:
            raise ValueError("titles should not empty!")
        self.rowCount = 0
        self.columnCount = len(titles)
        self.columns = []
        for i in titles:
            column = TableColumn(i)
            self.columns.append(column)

    def addRow(self, row):
        '''向表格添加一行內容
        row: 一行內容，是一個列表，每一項都是一個字符串'''
        if len(row) != self.columnCount:
            raise ValueError("lenth of row is not equeal as columnCount!")
        for i in range(0, len(row)):
            self.columns[i].add(row[i])
        self.rowCount = self.rowCount + 1

    def addEmptyRow(self):
        emptyRow = []
        for i in range(0, self.columnCount):
            emptyRow.append('')
        self.addRow(emptyRow)

    def clearAllRows(self):
        '''删除表格内除了标题以外的所有内容'''
        self.rowCount = 0
        for column in self.columns:
            column.clear()

    def show(self):
        # 打印表頭
        log = "+"
        for column in self.columns :
            stickCount = column.maxLen + 2
            for i in range(0, stickCount):
                log = log + "-"
            log = log + "+"
        print(log)           
        print("|", end="")
        for column in self.columns :
            maxLen = column.maxLen;
            titleLen = len(column.title)
            blankLen = maxLen - titleLen
            leftBlankLen = blankLen // 2
            rightBlankLen = blankLen - leftBlankLen
            print(" ", end="")
            for i in range(0, leftBlankLen) :
                print(" ", end="")
            print(column.title, end="")
            for i in range(0, rightBlankLen) :
                print(" ", end="")
            print(" |", end="")
        print("")            
        print(log) 

        # 打印內容
        for i in range(0, self.rowCount):
            print("|", end="")
            for j in range(0, self.columnCount):
                maxLen = self.columns[j].maxLen;
                contentLen = len(self.columns[j].content[i])
                blankLen = maxLen - contentLen
                leftBlankLen = blankLen // 2
                rightBlankLen = blankLen - leftBlankLen
                print(" ", end="")
                for k in range(0, leftBlankLen) :
                    print(" ", end="")
                print(self.columns[j].content[i], end="")
                for k in range(0, rightBlankLen) :
                    print(" ", end="")
                print(" |", end="")
            print("")      
        print(log) 
